<?php

namespace App\Console\Commands;

use App\Http\Models\Mysql\SsyPricingOrderTable;
use App\Http\Repository\SzdptechRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class pullPricingOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:pullPricingOrder {--number=} {--t73=}';

    /**
     * The console command description.
     * 0 8 * * *
     * @var string
     */
    protected $description = '拉取数加核价单数据（废弃）';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $autoKey = "auto_pullPricingOrder";
        Cache::put($autoKey, 1, Carbon::now()->addMinutes(15));
        $number = $this->option("number") ?? "";
        $t73 = $this->option("t73") ?? "";
        if(!empty($number)){
            $number = explode(",", $number);
        }
        if(!empty($t73)){
            $t73 = explode(",", $t73);
        }
        $res = SzdptechRepository::searchVerifyPrice($t73, $number);
        if(!isset($res['value']) || empty($res['value'])){
            Cache::forget($autoKey);
            die("获取核价单数据失败");
        }
        $total = count($res['value']);
        $num = 1;
        foreach ($res['value'] as $item){
            $save = [
                "pricing_number" => $item['number'],
                "type_no" => $item['style_number'],
                "type_name" => $item['style'],
                "valuation_purpose" => $item['t73'],
                "s_status" => $item['state'],
                "product_category" => $item['product_category'] ?? "",
                "style_detail_category" => $item['style_detail_category'] ?? "",
                "factory_name" => $item['supplier'] ?? "",
                "currency_id" => $item['currency_id'] ?? 1, //币种 1 人民币 2 美元
                "embroidery_factory" => $item['f8'] ?? "",
                "main_material_total" => $item['f69'] ?? "",
                "accessories_total" => $item['f70'] ?? "",
                "packing_material_total" => $item['f71'] ?? "",
                "process_cost_total" => $item['f72'] ?? "",
                "embroider_total"=>$item['f77'] ?? "",
                "estimate_total"=>$item['f78'] ?? "",
                "embroider_cost_total"=>$item['f62'] ?? "",
                "embroider_cost_plus_total"=>$item['f63'] ?? "",
                "management_rate"=>$item['f61'] ?? "",
                "garment_cost_total"=>$item['f64'] ?? "",
                "garment_cost_plus_total"=>$item['f65'] ?? "",
                "cost_total"=>$item['f66'] ?? "",
                "cost_plus_total"=>$item['f67'] ?? "",
                "orther_total"=>$item['f74'] ?? "",
                "orther_plus_total"=>$item['f75'] ?? "",
                "addtime"=>$item['creation_time'] ?? "",
                "adduser"=>$item['creator'] ?? "",
                "comment"=>$item['comment'] ?? "",
                "colour"=>$item['colour'] ?? "",
                "createtime"=>date("Y-m-d H:i:s")
            ];
            echo "第[{$total}, {$num}]页, 正在处理: ".$item['number'];
            $exist = (new SsyPricingOrderTable())->where('pricing_number', $item['number'])->exists();
            if($exist){
                $where = ['pricing_number' => $item['number']];
                unset($save['createtime']);
                unset($save['pricing_number']);
                (new SsyPricingOrderTable())->updateDataWhere($where, $save);
                echo " 已更新\n";
            }else{
                (new SsyPricingOrderTable())->createData($save);
                echo " 已新增\n";
            }
        }
        Cache::forget($autoKey);
    }
}
